#include "Player.h"
#define MAXWIDTH 640
#define MAXHEIGHT 480
//#include <iostream>
Player::Player()
{
    //ctor

    //Set Initial Position
    //Doesn't check if in bounds.
    rect.x = 0;
    rect.y = 0;
    rect.w = 20;
    rect.h = 20;
    steps = 0;
    health_max = 100;

    //Load Sprite
    sprite = image_load("down1.png");
}
Player::Player(int x_val, int y_val, int w_val, int h_val){
    rect.x = x_val;
    rect.y = y_val;
    rect.w = w_val;
    rect.h = h_val;
    sprite_file = "down1.png";
    steps = 0;
    sprite = image_load("down1.png");
    health_max = 100;

}

Player::Player(int x_val, int y_val, int w_val, int h_val, std::string filename){
    rect.x = x_val;
    rect.y = y_val;
    rect.w = w_val;
    rect.h = h_val;
    sprite_file = filename;
    steps = 0;
    sprite = image_load(sprite_file.c_str());
    health_max = 100;
}

Player::~Player()
{
    //SDL_FreeSurface(sprite);
    //dtor
}

void Player::set_h(int h_val) { rect.h = h_val; }
void Player::set_w(int w_val) { rect.w = w_val; }
void Player::set_x(int x_val) { rect.x = x_val; }
void Player::set_y(int y_val) { rect.y = y_val; }
void Player::set_steps(int step_val) { steps = step_val;}
void Player::set_health_max(int health_max_val) { health_max = health_max_val; }
int Player::get_h(){ return rect.h;}
int Player::get_w(){ return rect.w;}
int Player::get_x(){ return rect.x;}
int Player::get_y(){ return rect.y;}
int Player::get_steps() { return steps;}
int Player::get_health_max() { return health_max;}
void Player::set_position(int x_val, int y_val) {
    rect.x = x_val;
    rect.y = y_val;
}




void Player::Move() {


    //Do Fancy Math Stuff

}
void Player::Move(int direction, SDL_Surface* screen) {
    bool alt_step;
    steps % 2 == 0 ? alt_step = true : alt_step = false;
    //std::cout << steps << std::endl;
    switch(direction) {
        case SDLK_RIGHT:
            alt_step ? sprite_file = "right1.png" :
                       sprite_file = "right2.png";
            rect.x < (MAXWIDTH - rect.w) ?
                rect.x += 20 :
                rect.x = MAXWIDTH - rect.w;
            break;
        case SDLK_LEFT:
            alt_step ? sprite_file = "left1.png" :
                       sprite_file = "left2.png";
            rect.x > 0 ? rect.x -= 20 : rect.x = 0;

            break;
        case SDLK_UP:
            alt_step ? sprite_file = "up1.png" :
                       sprite_file = "up2.png";
            rect.y > 0 ? rect.y -= 20 : rect.y = 0;

            break;
        case SDLK_DOWN:
            alt_step ? sprite_file = "down1.png" :
                       sprite_file = "down2.png";
            rect.y < (MAXHEIGHT - rect.h) ?
                rect.y += 20 :
                rect.y = MAXHEIGHT - rect.h;

            break;
        default: break;

    }
    sprite = image_load(sprite_file.c_str());
    steps++;
    //Draw(screen);
    //SDL_BlitSurface(sprite, 0, screen, &rect);

    //SDL_FreeSurface(sprite);
    //SDL_FreeSurface(sprite);
}

void Player::Draw(SDL_Surface* screen) {
//Draw on screen
    //SDL_FreeSurface(sprite);

    SDL_BlitSurface(sprite, 0, screen, &rect);

}
void Player::Clean() {

    SDL_FreeSurface(sprite);

}
SDL_Surface* Player::image_load(std::string file_name) {
    SDL_Surface* loaded = NULL;
    SDL_Surface* optoed = NULL;
    loaded = IMG_Load(file_name.c_str());
    if(loaded != NULL) {
        optoed = SDL_DisplayFormatAlpha(loaded);
        SDL_FreeSurface(loaded);
    }
    return optoed;
}
